//
//  WMEArticleInformationViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/3/5.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEArticleInformationViewController.h"
#import "WMEArticleInformationTableViewCell.h"
#import "WMEArticleInformationModel.h"
#import "WMEArticleWebViewController.h"
#import <Masonry.h>
#import <AFNetworking.h>
#import <MJRefresh.h>

@interface WMEArticleInformationViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain)UIView *myHeadView;
@property (nonatomic, copy)NSString *urlStr;
@property (nonatomic, retain)NSNumber *next;
@end

@implementation WMEArticleInformationViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_myHeadView release];
    [_urlStr release];
    [_next release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self data];
    [self shuaXing];
    [self loadingFunction];
}
#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    __block __typeof(&*self)blockself = self;
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(blockself.view.mas_left).with.offset(0);
        make.right.equalTo(blockself.view.mas_right).with.offset(0);
        make.top.equalTo(blockself.view.mas_top).with.offset(0);
        make.bottom.equalTo(blockself.view.mas_bottom).with.offset(0);
    }];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView release];
}
#pragma mark -数据处理
- (void)data
{
    NSString *temp = @"http://api.meitianapp.com/api/v1/articles?filter=byAuthor&limit=10&order=asc&orderBy=priority&param=93fa1325-e4df-38a4-b1de-3a25ec00a702&start=";
    self.urlStr = [temp stringByReplacingOccurrencesOfString:@"93fa1325-e4df-38a4-b1de-3a25ec00a702" withString:_model.authorId];
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:_urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"result"];
        self.next = responseObject[@"next"];
        self.dataArr = [WMEArticleInformationModel modelHandlerWithArray:arr];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"有错误");
    }];
}
#pragma mark -tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableView registerClass:[WMEArticleInformationTableViewCell   class] forCellReuseIdentifier:@"articleInfo"];
    WMEArticleInformationTableViewCell *articleInfo = [_tableView dequeueReusableCellWithIdentifier:@"articleInfo" forIndexPath:indexPath];
    WMEArticleInformationModel *m = _dataArr[indexPath.row];
    articleInfo.model = m;
    return articleInfo;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WMEArticleModel *model = _dataArr[indexPath.row];
    NSString *strUrl = [@"http://www.meitianapp.com/app/article/8da944da-f954-3084-b29b-a93a500ab6b0" stringByReplacingOccurrencesOfString:@"8da944da-f954-3084-b29b-a93a500ab6b0" withString:model.articleId];
    WMEArticleWebViewController *webArticle = [[WMEArticleWebViewController alloc] init];
    webArticle.urlStr = strUrl;
    [self.navigationController pushViewController:webArticle animated:YES];
}
- (void)shuaXing
{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArr removeAllObjects];
        [self data];
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    }];
}
- (void)loadingFunction
{
    _tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
//        NSString *temp = [@"http://api.meitianapp.com/api/v1/articles?filter=byAuthor&limit=10&order=asc&orderBy=priority&param=93fa1325-e4df-38a4-b1de-3a25ec00a702&start=" stringByReplacingOccurrencesOfString:@"start=" withString:[NSString stringWithFormat:@"start=%@", _next]];
//        self.urlStr = [temp stringByReplacingOccurrencesOfString:@"93fa1325-e4df-38a4-b1de-3a25ec00a702" withString:_model.authorId];
        NSString *strNew = [_urlStr stringByReplacingOccurrencesOfString:@"start=" withString:[NSString stringWithFormat:@"start=%@", _next]];
        AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
        [data GET:strNew parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            nil;
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSArray *arr = responseObject[@"result"];
            self.next = responseObject[@"next"];
            NSArray *tempArr = [WMEArticleInformationModel modelHandlerWithArray:arr];
            [self.dataArr addObjectsFromArray:tempArr];
            [_tableView reloadData];
            if (_next != 0) {
                [_tableView.mj_footer endRefreshing];
            }else {
                [_tableView.mj_footer endRefreshingWithNoMoreData];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"有错误");
        }];

    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
